<?php

class CaptchaController extends BaseController {
        
    public function indexAction($requestData) {
    	#Now lets use md5 to generate a totally random string
		$md5 = md5(microtime() * mktime());
		
		#We dont need a 32 character long string so we trim it down to 5
		$string = substr($md5, 0, 5);
		$string = strtoupper($string);
		
		$captcha = imagecreatefrompng("./captcha.png");

		#Lets set the colours, the colour $line is used to generate lines.
		# Using a blue misty colours. The colour codes are in RGB
		$black = imagecolorallocate($captcha, 0, 0, 0);
		
		$font = 'fonts/You Are Loved.ttf';
		imagettftext($captcha, 30, rand(-7, 7), rand(45, 55), rand(45, 55), $black, $font, $string);
		
		#Encrypt and store the key inside of a session
		$_SESSION['key'] = md5($string);
		
		#Output the image
		header("Content-type: image/png");
		imagepng($captcha);
		imagedestroy($captcha);

    	exit;
    }
    
    public static function validateCaptcha($captcha) {
    	return ($_SESSION['key'] == md5($captcha));
    }

}

?>